var form;
var $;
var table;
var layer;
var display = '';
var imgType = '';
var tableRender;

$(document).click(function(){//子页面
    $(".rightmenu",window.parent.document).hide();
});

layui.use(['form','jquery','table','layer'],function() {
    form = layui.form;
    $ = layui.jquery;
    table = layui.table;
    layer = layui.layer;

    //监听下拉框
    form.on('select(filter)',function (data) {
        if (data.value == "0") {
            display = '';
        }
        if (data.value == "1"){
            display = 3;
        }
        if (data.value == "2") {
            display = 2;
        }
    });
    form.on('select(filter2)',function (data) {
        if (data.value == "0"){
            imgType = '';
        }
        if (data.value == "1"){
            imgType = "视频"
        }
        if (data.value == "2"){
            imgType = "商品";
        }
    });

    table.on('row(test)', function(obj){
        localStorage.setItem("advertisementData",JSON.stringify(obj.data));
    });

    //展示轮播图列表 （2显示/3不显示）
    advertisement(2,"");

});

function advertisement(display,imgType) {
    //获取登录人员id
    var user = JSON.parse(localStorage.getItem("user"));
    var userId = user.userId;
    $.ajax({
        url:'/Advertisement/getAdvertisement',
        methods: "post",
        dataType:'json',
        data: {"display": display, "imgType": imgType,"createId": userId},
        success:function (data) {
            tab(data.data);
        }
    });
}

function tab(data){
    tableRender = table.render({
        id: "idTest",
        elem: '#demo' ,
        data:data,
        page:true,
        limit: 4,
        cols:[[
            {checkbox:true},
            {field:'createId',title:'用户ID',align:'center',height:'20px',unresize:true},
            {field:'imgAddress',title:'图片地址',width:110,align:'center',unresize:true,templet:function(data){
                    return "<img id='imgSrc' src='"+data.imgAddress+"'width='80px' height='80px'/>";
                }},
            {field:'createName',title:'创建人',align:'center',unresize:true},
            {field:'imgTitle',title:'图片标题',align:'center',unresize:true},
            {field:'imgType',title:'图片类型',align:'center',unresize:true},
            {field:'remarks',title:'备注',align:'center',unresize:true},
            {field:'startDate',title:'开始时间',width:170,align:'center',unresize:true},
            {field:'expireDate',title:'到期时间',width:170,align:'center',unresize:true},
            {field:'',title:'操作',align:'center',width:100,unresize:true,templet:function(data){
                    return "<a class='operation' onclick='edit("+data.carouselId+")'>编辑</a>&nbsp&nbsp"+
                        "<a onclick='delProduct(\""+data.carouselId+"\")' class='operation'>删除</a>";
                }},
        ]],
    });
}

//编辑轮播图
function edit(id) {
    $("#proId").val(id);
    location.href="/Advertisement/advertisement_list_update.html"
}

//删除轮播图
function delProduct(id) {
    layer.open({
        type:1,
        title: "提示信息",
        content:"<div style='font-size: 20px ; color: red; margin:30px  ;'>是否删除此轮播图?</div>",
        area:['300px','200px'],
        btn:["确定","取消"],
        yes: function (index,layero) {
            $.ajax({
                url:'/Advertisement/updateAdvertisement',
                dataType:'json',
                data:{"carouselId":id},
                success:function (data) {
                    if(data){
                        layer.msg("删除成功");
                    }else{
                        layer.msg("删除失败");
                    }
                    advertisement("","");
                    layer.close(index);
                }
            });
        },
        btn2: function (index,layero) {
            layer.close(index);
        }
    });
}

//刷新页面
function refresh() {
    window.location.reload();
}

//搜索
$("#selAdver").on("click",function () {
    $("#display").val(0);
    $("#imgType").val(0);
    //重新渲染表单
    form.render();
    advertisement(display, imgType);
    display = '';
    imgType = '';
});

//重置
$("#reset").on("click",function () {
    $("#display").val(0);
    $("#imgType").val(0);
    form.render();
    advertisement('', '');
});

//返回
function backOff() {
    var arr = localStorage.getItem("numArr");
    var numArr = arr.split(",");
    numArr.pop();
    window.parent.layui.element.tabChange('tab',numArr[numArr.length - 1]);
    localStorage.setItem("numArr",numArr);
}
